package com.newedu.healthtest.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.*;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.net.URI;
import java.nio.charset.Charset;

/**
 * @author steven
 */
public class HttpClientUtil {

    private static CloseableHttpClient httpClient = null;
    private static CloseableHttpResponse response = null;

    static {
        httpClient = HttpClients.createDefault();
    }

    /**
     * GET API
     */
    public static CloseableHttpResponse getApi(String token, String url, JSONObject parameters) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        if(parameters!=null){
            parameters.entrySet().forEach(item -> uriBuilder.setParameter(item.getKey(), String.valueOf(item.getValue())));
        }
        URI uri = uriBuilder.build();
        HttpGet httpGet = new HttpGet(uri);
        httpGet.setHeader("token", token);
        return httpClient.execute(httpGet);
    }

    /**
     * POST JSON API
     */
    public static CloseableHttpResponse postJsonApi(String token, String url, JSONObject parameters) throws Exception {
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-type","application/json; charset=utf-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("token", token);
        httpPost.setEntity(new StringEntity(parameters.toJSONString(), Charset.forName("UTF-8")));
        return httpClient.execute(httpPost);
    }

    /**
     * PUT JSON API
     */
    public static CloseableHttpResponse putJsonApi(String token, String url, JSONObject parameters) throws Exception {
        HttpPut httpPut = new HttpPut(url);
        httpPut.addHeader("Content-type","application/json; charset=utf-8");
        httpPut.setHeader("Accept", "application/json");
        httpPut.setHeader("token", token);
        httpPut.setEntity(new StringEntity(parameters.toJSONString(), Charset.forName("UTF-8")));
        return httpClient.execute(httpPut);
    }

    /**
     * PUT JSON API
     */
    public static CloseableHttpResponse putJsonApi(String token, String url, JSONArray parameters) throws Exception {
        HttpPut httpPut = new HttpPut(url);
        httpPut.addHeader("Content-type","application/json; charset=utf-8");
        httpPut.setHeader("Accept", "application/json");
        httpPut.setHeader("token", token);
        httpPut.setEntity(new StringEntity(parameters.toJSONString(), Charset.forName("UTF-8")));
        return httpClient.execute(httpPut);
    }

    /**
     * DELETE API
     */
    public static CloseableHttpResponse deleteApi(String token, String url, JSONObject parameters) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        if(parameters!=null){
            parameters.entrySet().forEach(item -> uriBuilder.setParameter(item.getKey(), String.valueOf(item.getValue())));
        }
        URI uri = uriBuilder.build();
        HttpDelete httpDelete = new HttpDelete(uri);
        httpDelete.setHeader("token", token);
        return httpClient.execute(httpDelete);
    }


    /**
     * DELETE API
     */
    public static CloseableHttpResponse GetRquest(String url) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);

        uriBuilder.setParameter("name", "1");
        uriBuilder.setParameter("pwd", "1");
        uriBuilder.setParameter("acct", "1");
        uriBuilder.setParameter("userAcct", "1");
        uriBuilder.setParameter("userPwd", "1");
        uriBuilder.setParameter("sort", "1");
        uriBuilder.setParameter("title", "1");
        uriBuilder.setParameter("chnl_code", "1");
        uriBuilder.setParameter("newsTitile", "1");
        uriBuilder.setParameter("channelid", "1");
        uriBuilder.setParameter("newsContext", "1");
        uriBuilder.setParameter("id", "1");

        URI uri = uriBuilder.build();
        HttpGet httpGet = new HttpGet(uri);
        return httpClient.execute(httpGet);
    }
}